<template>
  <div class="box">
    <el-tabs type="border-card" class="card1">
      <el-tab-pane label="个人信息">
        <div class="body">
          
          <div class="left">
            
            <el-avatar shape="circle" style="border-radius: 50%;border: 1px solid black" :size="180" :src="`http://localhost:8088/image/image/${accountInfoForm.avatar}`"></el-avatar>
            <div class="accountbtn">
              <el-upload
                  action="http://localhost/flowerInfo/upload"
                  :on-success="handlePictureName"
                  :file-list="accountInfoForm.fileList">
              <el-button icon="el-icon-s-custom" style="width: 180px;">修改头像</el-button>
              </el-upload>
            </div>
          </div>
          
          <div class="right" >
            <el-form :model="accountInfoForm" label-width="130px" ref="accountInfoForm" :rules="rules">
              <el-form-item label="用户编号">
                <el-input v-model="accountInfoForm.accountId" style="width: 300px" disabled></el-input>
              </el-form-item>
              <el-form-item label="用户名称">
                <el-input v-model="accountInfoForm.nickName" style="width: 300px"></el-input>
              </el-form-item>
              <el-form-item label="用户类型">
                <el-input v-model="accountInfoForm.accountType" style="width: 300px" disabled></el-input>
              </el-form-item>
              <el-form-item label="用户邮箱">
                <el-input v-model="accountInfoForm.email" style="width: 300px"></el-input>
              </el-form-item>
              <el-form-item label="用户电话" prop="phoneNumber">
                <el-input v-model="accountInfoForm.phoneNumber" style="width: 300px"></el-input>
              </el-form-item>
              <el-form-item label="用户最后登录时间">
                <el-input v-model="accountInfoForm.loginDate" style="width: 300px" disabled></el-input>
              </el-form-item>
              <el-input v-model="accountInfoForm.avatar" style="display: none"></el-input>
              <el-form-item style="position: absolute;top:380px;left: -250px;">
                <el-button type="primary" @click="submitForm">保存个人信息</el-button>
              </el-form-item>
            </el-form>
          </div>
          
        </div>
        
      </el-tab-pane>
      <el-tab-pane label="修改密码">
        <div class="body">
          <div style="position: absolute;top: 80px;left: 400px">
          <el-form :model="accountInfoForm" label-width="130px" ref="ref" :rules="rules">
            <el-form-item label="用户账号" >
              <el-input v-model="accountInfoForm.accountName" style="width: 300px" disabled></el-input>
            </el-form-item>
              <el-input v-model="accountInfoForm.password" style="width: 300px;display: none"></el-input>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="accountInfoForm.newPassword" style="width: 300px" type="text"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input v-model="accountInfoForm.confirmPassword" style="width: 300px" type="text"></el-input>
            </el-form-item>
            <el-form-item style="margin-left: 80px;margin-top: 40px">
              <el-button type="primary" @click="submitPasswordForm">保存密码</el-button>
            </el-form-item>
            
          </el-form>
          </div>
        </div>
      </el-tab-pane>
      
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: "SelfView",
  data(){

    var validatePassword = (rule, value, callback) => {
      if (value !== this.accountInfoForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else if(value === this.accountInfoForm.password) {
        callback(new Error('近期已使用过改密码，请修改!'));
      }
    };
    
    return {
      accountInfoForm: {
        accountId:0,
        accountName:sessionStorage.getItem("accountName"),
        password:'',
        nickName:'',
        accountType:'',
        email:'',
        phoneNumber:'',
        avatar:'',
        loginDate:'',
        createTime:'',
        fileList:[],
        
        newPassword:'',
        confirmPassword:'',
      },
      

      rules:{
        newPassword: [
          { required: true, message: '请填写新密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请填写确认密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        phoneNumber:[
          { required: true, message: '请填写手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11个数字', trigger: 'blur' }
        ],
        email:[
          { required: true, message: '请填写邮箱', trigger: 'blur' },
        ],
        nickName:[
          { required: true, message: '请填写用户名称', trigger: 'blur' },
        ],
      },
    }
  },
  
  methods:{
    //图片上传成功后的返回图片名称
    handlePictureName(response){
      this.accountInfoForm.avatar = response;
      console.log(this.accountInfoForm.avatar)
      this.accountInfoForm.fileList = [];
    },
    
    selectAccountInfo() {
      this.$axios.get("/account/selectAccountByAccountName",{
        params:{
          accountName: this.accountInfoForm.accountName,
        }
      }).then(response=>{
        if (response.data.code ===200){
          this.accountInfoForm = response.data.data;
          this.$message({
            type: "success",
            message: "查询用户信息成功",
          });
        }
      })
    },
    //提交个人信息修改
    submitForm(){
      this.$axios.post("/account/updateAccountByAccountId",{
        accountId: this.accountInfoForm.accountId,
        accountName: this.accountInfoForm.accountName,
        password: this.accountInfoForm.password,
        nickName: this.accountInfoForm.nickName,
        accountType: this.accountInfoForm.accountType,
        email: this.accountInfoForm.email,
        phoneNumber: this.accountInfoForm.phoneNumber,
        avatar: this.accountInfoForm.avatar,
      }).then(response=>{
        if (response.data.code === 200){
          
          this.$message({
            type: "success",
            message: "修改用户信息成功",
          });
        }
      })
    },
    //提交密码修改
    submitPasswordForm(){
      this.$axios.post("/account/updateAccountByAccountId",{
        accountId: this.accountInfoForm.accountId,
        password: this.accountInfoForm.newPassword,
      }).then(response=>{
        if (response.data.code === 200){
          this.$message({
            type: "success",
            message: "修改密码成功",
          });
        }
      })
    },
  },
  
  created() {
    this.selectAccountInfo();  // 查询初始用户信息
  }
}
</script>

<style scoped lang="less">
.box {
  border-radius: 4px;
  border: 1px solid white;
  background-color: white;
  margin: 30px 200px;
}

.box .card1 {
  margin: 20px 10px;
  text-align: left;
}

.body {
  margin: 10px 150px;
  height: 400px;
}

.body .left{
  margin-left: 120px;
  margin-top: 80px;
  width: 200px;
 
}

.accountbtn{
  margin-top: 50px;
}

.body .right{
  position: absolute;
  top:65px;
  left: 650px;
}
</style>